package br.uff.probabilidade.modelo.configuracao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Vector;

import br.uff.probabilidade.modelo.configuracao.exception.LayoutInvalido;

public class Configuracao {	
	private String path;
	
	private Vector layouts = new Vector();
	
	public Configuracao(String path){
		this.path = path;
	}
	
	public void addLayout(Layout layout){
		this.layouts.add(layout);
	}
	
	public Layout getLayout(int i){
		return (Layout)this.layouts.get(i);
	}
	
	public void setLayout(){
		try{
			BufferedReader input =  new BufferedReader(new FileReader(new File(this.path)));
			
			Layout layout;
			String line;
			while (( line = input.readLine()) != null){
				layout = new Layout(' ');
				layout.extrairValores(line);
				this.addLayout(layout);
			}

			
		}catch (LayoutInvalido e) {
			e.printErro();		
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String[] getParametrosOfCampo(String campo){
		Vector<String> params = new Vector<String>();
		for(int i=0;i<this.layouts.size();i++){
			if(this.getLayout(i).getCampo().equals(campo)){				
				for(int j=0;j<this.getLayout(i).getParametros().length;j++){
					params.add(this.getLayout(i).getParametros()[j]);
				}
			}
		}
		String[] retorno = new String[params.size()];
		for(int i=0;i<params.size();i++){
			retorno[i] = params.get(i);
		}			
		return retorno;
	}
	
	
}
